﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CommDll;
using System.Threading;   
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
using DevComponents.DotNetBar;

namespace HospitalMedicineTraceability
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //处理未捕获的异常
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理非UI线程异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            // 00 MessageEx汉化
            LocalizationKeys.LocalizeString += CommFun._LocalizeString;
            // 01 是否唯一运行
            if (AppInfo._runningInstance() == null)
            {
                // 02 不更新打开程序
                if (!AppInfo._isUpdate())
                {
                    // 02 运行新实例
                    Application.Run(new frmSplash());
                    if (AppInfo.m_bDbConnected)
                    {
                        frmLogin frm = new frmLogin();
                        if (frm.ShowDialog() == DialogResult.OK)
                        {
                            Application.Run(new frmMain());
                            // 是否注销重新登录
                            while (!AppInfo.m_bLogin) {
                                AppInfo.m_bLogin = true;
                                Application.Run(new frmMain());
                            }
                        }
                    }
                }
            }
            else
            {
                CommFun._messageBoxShow(string.Format("【{0}】已经运行！", AppInfo.m_strAppName), string.Format("【{0}】-系统提示！", AppInfo.m_strAppName), MessageBoxButtons.OK, MessageBoxIcon.Information);
            }            
        }

        #region 1.异常处理
        /// <summary>
        /// 全局异常处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show("Application_ThreadException:" + e.ExceptionObject.ToString());
        }

        /// <summary>
        /// 全局异常处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show("Application_ThreadException:" + e.Exception.Message);
        }
        #endregion
    }
}
